<style lang="less" scoped>
  .mainView {
    width: 100%;
    height: 100%;
    .sidebar {
      position: fixed;
      height: 100%;
      overflow: auto;
      transition: width 0.3s;
      border-right: solid 1px #e6e6e6;
      z-index: 120;
    }
    .toolbar {
      position: fixed;
      padding-left: 200px;
      display: flex;
      align-items: center;
      width: 100%;
      height: 40px;
      box-shadow: 0 2px 1px 1px rgba(100, 100, 100, 0.1);
      transition: padding 0.3s;
      z-index: 100;
      .icon-expand {
        padding: 10px;
        cursor: pointer;
        transition: all 0.3s;
        &.expanded {
          transform: rotateZ(90deg);
          transition: all 0.3s;
        }
      }
      .tools {
        position: absolute;
        right: 200px;
        width: 200px;
        height: 100%;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }

      .system__title {
        padding-left: 20px;
        font-size: 14px;
      }
      .user__wrapper {
        position: absolute;
        height: 100%;
        width: 200px;
        // background: red;
        padding: 10px;
        top: 0;
        right: 0;
        display: flex;
        justify-content: space-between;
        align-items: center;
      }
    }
    .viewFramework {
      position: absolute;
      top: 40px;
      right: 0;
      bottom: 0;
      transition: left 0.3s;
      .singlePageView {
        position: relative;
        background: #fff;
        width: 100%;
        height: 100%;
        z-index: 60;
        padding-top: 0;
        &.viewPadding {
          padding-top: 40px;
        }
        .pageContainer {
          position: relative;
          width: 100%;
          height: 100%;
          padding: 10px;
        }
      }
    }
  }
</style>

<template>
  <div class="mainView">
    <div :style="{paddingLeft: isCollapse?'64px':'200px'}" class="toolbar">
      <i :class="{expanded: !isCollapse}" @click="handleSidebarCollapse" class="iconfont icon-expand"></i>

      <div>
        <span class="system__title">厂办大集体系统</span>
      </div>

      <!-- <div class="tools"> -->
      <!-- <language></language> -->
      <!-- </div> -->
      <div class="user__wrapper">
        <div class="user__avator"></div>
        <el-dropdown trigger="click" size="medium" @command="handleCommand">
          <span class="el-dropdown-link" style="cursor: pointer; color: #2d8cf0;">
            {{userInfo.fullname}}
            <i class="el-icon-arrow-down el-icon--right"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="changePassword">{{$t('operation.changePassword')}}</el-dropdown-item>
            <el-dropdown-item command="logout" divided>{{$t('operation.logout')}}</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
    <div class="sidebar">
      <sidebar :is-collapse="isCollapse" />
    </div>
    <div :style="{left: isCollapse?'64px':'200px'}" class="viewFramework">
      <page-tag-controller v-if="hasPageTagController && $route.name !== 'home'" :opened-page-list="openedPageList"></page-tag-controller>
      <div :class="{viewPadding: hasPageTagController && $route.name !== 'home'}" class="singlePageView">
        <div class="pageContainer">
          <keep-alive :include="cachedPageNameList">
            <router-view></router-view>
          </keep-alive>
        </div>
      </div>
    </div>
    <!-- 修改密码 -->
    <el-dialog :visible.sync="passwordShowflag" append-to-body :close-on-click-modal="false" :close-on-press-escape="false" @open="handleDialogOpen" @closed="handleDialogClose">
      <div slot="title">{{$t('operation.changePassword')}}</div>
      <div>
        <simple-form ref="form" v-model="formModel" :form-field="formField" label-width="90px" :grid="{span: 24}">
        </simple-form>
      </div>
      <div slot="footer">
        <el-button @click="handleDialogClose">{{$t('operation.cancel')}}</el-button>
        <el-button type="primary" @click="handleDataSubmit">{{$t('operation.submit')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
const BASEURL = {
  changePassword: '/ajax.html?sys_method=ChangePassword'
}
export default {
  name: 'main-view',
  data () {
    return {
      isCollapse: true,
      // 隐藏页面标签栏
      hasPageTagController: true,
      passwordShowflag: false,
      formModel: {}
    }
  },
  methods: {
    handleSidebarCollapse () {
      this.isCollapse = !this.isCollapse
    },

    handleCommand (command) {
      if (command === 'logout') {
        this.$store.commit('systemLogout')
        this.$router.replace({ name: 'login' })
      } else if (command === 'changePassword') {
        this.passwordShowflag = true
      }
    },

    handleDialogOpen () {
      this.formModel = {}
    },

    handleDialogClose () {
      this.$refs.form.resetFields()
      this.passwordShowflag = false
    },

    handleDataSubmit () {
      this.$refs.form.$refs.simpleForm.validate(status => {
        if (status) {
          let loadingFlag = this.$loading({ target: this.$el.querySelector('.el-dialog') })
          this.$axios
            .post(BASEURL.changePassword, { pwd: this.formModel.pwd })
            .then(resp => {
              this.$message.success(this.$t('tip.saveSuccess'))
              this.passwordShowflag = false
            })
            .finally(() => {
              loadingFlag.close()
            })
        }
      })
    }
  },
  computed: {
    userInfo () {
      return this.$store.state.app.userInfo
    },
    openedPageList () {
      return this.$store.state.app.openedPageList
    },
    cachedPageNameList () {
      // 如果隐藏页面标签栏，则不需要缓存历史打开的目录
      if (!this.hasPageTagController) return []
      return ['home'].concat(this.$store.state.app.openedPageList.map(item => {
        return item.name
      }))
    },
    formField () {
      return [
        {
          prop: 'pwd',
          label: this.$t('field.newPassword'),
          type: 'Input',
          rules: { trigger: 'blur', message: this.$t('validate.notBlank'), required: true }
        },
        {
          prop: 'confirmPwd',
          label: this.$t('field.confirmPassword'),
          type: 'Input',
          rules: [
            { required: true, message: this.$t('validate.inputPasswordAgain'), trigger: 'blur' },
            {
              validator: (rule, value, callback) => {
                if (value !== this.formModel.pwd) {
                  callback(new Error(this.$t('validate.passwordNotMatch')))
                } else {
                  callback()
                }
              },
              trigger: 'blur'
            }
          ]
        }
      ]
    }
  }
}
</script>
